<#include "/common/include.html"/>
<@pageTheme mark="${config.optimize()?string('true', 'false')}">
<@header title="修改用户"/>
<@addorEdit fromId="form-user-edit">   
    <input type="hidden" id="userId" name="userId" value="${user.userId?default("")}"/>
    <input type="hidden" id="compId" name="compId" value="${user.compId?default("")}"/>
    <input type="hidden" id="deptId" name="deptIdList" value=""/>
    <div class="row">
        <div class="col-sm-6">
            <div class="form-group">
                <label class="col-sm-4 control-label"><span style="color: red; ">*</span>登录账号：</label>
                <div class="col-sm-8">
                    <input id="username" name="username" value="${user.username?default("")}" readonly="true" placeholder="请输入登录账号" class="form-control" type="text">
                </div>
            </div>
        </div>
        <div class="col-sm-6">
            <div class="form-group">
                <label class="col-sm-4 control-label"><span style="color: red; ">*</span>归属公司：</label>
                <div class="col-sm-8">
                    <div class="input-group">
                        <input name="compName"  id="compName" value="${user.compName?default("")}" type="text" placeholder="请选择归属公司" onclick="selectCompTree()" class="form-control" required>
                        <span class="input-group-addon"><i class="fa fa-fw fa-search"></i></span>
                    </div>
                </div>
            </div>
        </div>
    </div>
    <div class="row">
        <div class="col-sm-6">
            <div class="form-group">
                <label class="col-sm-4 control-label"><span style="color: red; ">*</span>用户姓名：</label>
                <div class="col-sm-8">
                    <input name="name" id="name" value="${user.name?default("")}"  placeholder="请输入用户名称" class="form-control" type="text" maxlength="30" required>
                </div>
            </div>
        </div>
        <div class="col-sm-6">
            <div class="form-group">
                <label class="col-sm-4 control-label"><span style="color: red; ">*</span>管理地区：</label>
                <div class="col-sm-8">
                    <div class="input-group">
                        <input name="deptName"  id="deptName"  type="text"  onclick="selectDeptTree()"  placeholder="请选择管理地区" class="form-control" required>
                        <span class="input-group-addon"><i class="fa fa-fw fa-search"></i></span>
                    </div>
                </div>
            </div>
        </div>
    </div>
    <div class="row">
        <div class="col-sm-6">
            <div class="form-group">
                <label class="col-sm-4 control-label"><span style="color: red; ">*</span>手机号码：</label>
                <div class="col-sm-8">
                    <input id="mobile" name="mobile" value="${user.mobile?default("")}" placeholder="请输入手机号码" class="form-control" type="text" maxlength="11" required>
                </div>
            </div>
        </div>
        <div class="col-sm-6">
            <div class="form-group">
                <label class="col-sm-4 control-label"><span style="color: red; ">*</span>所属角色：</label>
                <div class="col-sm-8">
                    <select id="_role" class="form-control select2-multiple" multiple required>
                        <#list roles  as role>
                            <option  value="${role.roleId}" <#if role.flag >selected = "selected"</#if>>${role.roleName}</option>
                        </#list>
                    </select>
                </div>
            </div>
        </div>
    </div>
    <div class="row">
        <div class="col-sm-6">
            <div class="form-group">
                <label class="col-sm-4 control-label">邮箱：</label>
                <div class="col-sm-8">
                    <input id="email" name="email" value="${user.email?default("")}" class="form-control email" type="text" maxlength="50" placeholder="请输入邮箱">
                </div>
            </div>
        </div>
        <div class="col-sm-6">
            <div class="form-group">
                <label class="col-sm-4 control-label">用户状态：</label>
                <div class="col-sm-8">
                    <label class="toggle-switch switch-solid">
                        <input type="checkbox" id="status" <#if user.status == '0'>checked="true"</#if>">
                        <span></span>
                    </label>
                </div>
            </div>
        </div>
    </div>
    <div class="row">
        <div class="col-sm-12">
            <div class="form-group">
                <label class="col-xs-2 control-label">卡号：</label>
                <div class="col-xs-4">
                    <input id="cardId" name="cardId" value="${user.cardId?default("")}" class="form-control" type="text" maxlength="30" placeholder="请输入卡号">
                </div>
            </div>
        </div>
    </div>
</@addorEdit>
<@footer>
<script>
    var deptName = "",deptIdList="";
    <#list deptList as detp>
        deptName+= "${detp.name?default("")}" + ",";
        deptIdList+= "${detp.deptId?default("")}" + ",";
    </#list>

    /* 校验表单 */
    $("#form-user-edit").validate({
        onkeyup: false,
        rules:{
            mobile:{
                isMobile: true,
                remote: {
                    url: baseURL + "sys/user/checkMobileUnique",
                    type: "POST",
                    dataType: "html",           /* 接受数据格式 */
                    data: {
                        "mobile": function() {
                            return opt.common.trim($("#mobile").val());
                        },
                        "userId":function() {
                            return opt.common.trim($("#userId").val());
                        }
                    },
                    dataFilter: function(data, type) {
                        return $.validate.unique(data);
                    }
                }
            }
        },
        messages: {
            "mobile":{
                remote: $.i18n.prop("sys.msg.mobile.already")
            }
        },
        focusCleanup: true
    });

    $(function() {
        $('#_role').select2({
            placeholder:"请选择岗位",
            allowClear: true
        });
        $("#deptName").val(deptName.substr(0,deptName.length-1));
        $("#deptId").val(deptIdList.substr(0,deptIdList.length-1));
    })

    /*提交*/
    function submitHandler(index, layero){
        if (opt.validate.form()) {
            var options = {
                id:'form-user-edit',
                delete:['compName','deptName'], /* 需要删除提交表单数据 */
                conversion:[ /*//需要转数据*/
                    {
                        name:'deptIdList',
                        value:'arr'
                    },
                    {
                        name:'userId',
                        value:'num'
                    }
                ]
            };
            var data = opt.form.dataUp(options);
            var postIds = opt.form.selectSelects("_role");
            var status = $("input[id='status']").is(':checked') == true ? 0 : 1;
            data.push({"name": "status", "value": status});
            data.push({"name": "roleIdList", "value": postIds});
            opt.operate.save(baseURL+'sys/user/edit',data);
        }
    }

    /* 获取公司树 */
    function selectCompTree() {
        var treeId = $("#compId").val();
        var compId = opt.common.isEmpty(treeId) ? "-1" : $("#compId").val();
        var url = baseURL + "sys/comp/selectCompTree/" + compId;
        var options = {
            title: $.i18n.prop("sys.select.comp"),
            width: "380",
            url: url,
            callBack: CompDoSubmit
        };
        opt.modal.openOptions(options);
    }
    function CompDoSubmit(index, layero){
        var _v = $.tree.callBackTree(layero,index);
        $("#compId").val(_v.id);
        $("#compName").val(_v.name);
        $("#deptName").val("");
        layer.close(index);
    }
    /* 获取公司对应地区 */
    function selectDeptTree(){
        var options = {
            title: $.i18n.prop("sys.select.dept"),
            width: "380",
            url: baseURL+'sys/dept/selectDeptTree',
            obj:{"compId":$("#compId").val(),"list":$("#deptId").val()},
            callBack: DeptDoSubmit
        };
        opt.modal.openOptions(options);
    }
    function DeptDoSubmit(index, layero){
        var _v = $.tree.callBackTree(layero,index);
        $("#deptId").val(_v.id);
        $("#deptName").val(_v.name);
        layer.close(index);
    }
</script>
</@footer>
</@pageTheme>